import axios from "./axios";


export const initMenu = (router, store) => {
    if(store.state.menu.length > 0){
        return;
    }else{
        //console.log("申请菜单前的userid:",store.userId);
        axios.post('/findAllMenu',store.state.user.userId).then(resp=>{
            //console.log("待验证用户id"+store.userId);
            if(resp){
                let fmtRoutes = formatRoutes(resp.data);
                console.log("经历formatRoutes之后的数据：",fmtRoutes);
                fmtRoutes.forEach(fmtRoute=>{
                    router.addRoute(fmtRoute);
                });
                //router.addRoute(fmtRoutes);
                const routes = router.getRoutes();
                //console.log("route:"+routes);
                // routes.forEach(route=>{
                //     console.log("进入foreach",route);
                // });
                store.commit('setMenu',fmtRoutes);
                console.log("菜单初始化",store.state.menu);
            }
        })
    }
};


export const formatRoutes = (routes) => {
    console.log("routes判断：",routes);
    let fmtRoutes = [];
    routes.forEach(router =>{
        // console.log("进入foreach",router);
        let {
            menuId,
            url,
            menuName,
            fatherPath,
            isLeaf,
            children
        }=router;
        // console.log(router.fatherPath);
        if (children && children instanceof Array && children.length>0){
            console.log("进入children判断");
            children = formatRoutes(children);
        }
        // console.log(url.substring(url.lastIndexOf('/')+1,url.length));
        let fmtRouter = {
            menuId:menuId,
            //url:url.substring(url.lastIndexOf('/'),url.length),
            path:url.substring(url.lastIndexOf('/'),url.length),
            name:menuName,
            menuName:menuName,
            meta:{title:menuName},
            children:children,
            // component:()=>{
            //     const componentName = url.substring(url.lastIndexOf('/')+1,url.length);
            //     try{
            //         if(componentName.startsWith("User")){
            //             return import('../views/User/'+url.substring(url.lastIndexOf('/')+1,url.length));
            //         }
            //         return import('../views/'+url.substring(url.lastIndexOf('/')+1,url.length));
            //     } catch(e){
            //         console.log("捕获异常");
            //         return null;
            //     }
                
            // }
            
           //component:import('@/views/User/UserList.vue')
           component:loadView(url)

        }
        fmtRoutes.push(fmtRouter);
    })
    // console.log("fmtRouter判断:",fmtRoutes);
    return fmtRoutes;
};

function loadView(url){
    if(url.substring(url.lastIndexOf('/')+1,url.length).startsWith("User")){
        console.log("进入User");
        //console.log('../views/User/'+url.substring(url.lastIndexOf('/')+1,url.length)+'.vue');
        return ()=>import('../views/User/'+url.substring(url.lastIndexOf('/')+1,url.length)+'.vue');
        //return url.substring(url.lastIndexOf('/')+1,url.length);
    }
    else{
        console.log("无实际意义页面"+url.substring(url.lastIndexOf('/')+1,url.length));
        //return modules[url];
        return ()=>import('../views/HomeView'+".vue");
    }
}